package com.zrc.module_find.activity.defrecycleview.demo2

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmFragment
import com.zrc.module_find.R
import com.zrc.module_find.BR
import com.zrc.module_find.activity.defrecycleview.DemoResponse
import com.zrc.module_find.databinding.FragmentDemo2Binding
import com.zrc.module_find.viewmodel.FindVM
import com.zrc.superframe_ui.stateview.state.EmptyState
import com.zrc.superframe_ui.stateview.state.ErrorState
import com.zrc.superframe_ui.stateview.state.LoadingState
import com.zrc.superframe_ui.stateview.state.SuccessState

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@SuppressLint("NewApi")
@Route(path = ARouterHub.FRAGMENT_DEMO2, name = "不一样过")
class Demo2Fragment : BaseMvvmFragment<FragmentDemo2Binding, FindVM>() , View.OnScrollChangeListener {

    lateinit var leftAdapter: Adapter1

    lateinit var rightAdapter: Adapter2

    private var list = arrayListOf<String>()

    private var page: Int = 1

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.fragment_demo2

    override fun getViewModel() = FindVM(this)

    override fun initObserver() {
        super.initObserver()
        mBinding.container.show<LoadingState>()//初始菊花屏
        mBinding.smartRefreshLayout.setOnRefreshListener {
            list.clear()
            mViewModel.getData(true, page)
        }

        mBinding.smartRefreshLayout.setOnLoadMoreListener {
            mViewModel.getData(false, page)
        }

        // 设置两个水平控件的联动
        mBinding.shsRvRightTitle.setScrollChangeListener(this)
        mBinding.shsRvRight.setScrollChangeListener(this)
        leftAdapter = Adapter1()
        leftAdapter.setOnItemChildClickListener { adapter, view, position ->
            onItemChildClickListener(view, leftAdapter.data[position], position)
        }
        rightAdapter = Adapter2()
        rightAdapter.setOnItemChildClickListener { adapter, view, position ->
            onItemChildClickListener(view, leftAdapter.data[position], position)
        }
        mBinding.rvLeft.adapter = leftAdapter
        mBinding.rvRight.adapter = rightAdapter
        mBinding.rvLeft.layoutManager = LinearLayoutManager(context)
        mBinding.rvRight.layoutManager = LinearLayoutManager(context)
        mBinding.rvLeft.isNestedScrollingEnabled = false
        mBinding.rvRight.isNestedScrollingEnabled = false
        mViewModel.getData(true, page)

        mViewModel.demoLiveData.observe(this, {
            if (it.isRefrensh)
                mBinding.smartRefreshLayout.finishRefresh()
            else
                mBinding.smartRefreshLayout.finishLoadMore()

            if (it.isRefrensh) {
                leftAdapter.setList(it.data)
                rightAdapter.setList(it.data)
            } else {
                leftAdapter.addData(it.data)
                rightAdapter.addData(it.data)
            }

            when (it.code) {
                -1 -> {
                    if (leftAdapter.data.isNullOrEmpty() && rightAdapter.data.isNullOrEmpty()) {
                        mBinding.container.show<ErrorState> {
                            it.setErrorIcon(R.drawable.vector_error)
                            it.setErrorMsg("出错了")
                            it.retry {
                                mBinding.smartRefreshLayout.autoRefresh()
                            }
                        }
                    }
                }
                -2 -> {
                    if (leftAdapter.data.isNullOrEmpty() && rightAdapter.data.isNullOrEmpty()) {
                        mBinding.container.show<EmptyState> {
                            it.setEmptyIcon(R.drawable.vector_error)
                            it.setEmptyMsg("暂无数据")
                        }
                    }
                }
                else -> {
                    mBinding.container.show<SuccessState>()
                }
            }
            page++
        })
    }

    private fun onItemChildClickListener(view: View, item: DemoResponse.OtherListBean, position: Int) {
        if (view.id==R.id.tv_name){
            ToastUtils.showLong("$position")
        }else{
            ToastUtils.showLong(item.author_name)
        }
    }

    override fun onScrollChange(p0: View?, p1: Int, p2: Int, p3: Int, p4: Int) {
        mBinding.shsRvRightTitle.scrollTo(p1,0)
        mBinding.shsRvRight.scrollTo(p1,0)
    }
}